<div style="width:670px; float:left; padding-left:5px; margin-left:5px" class="border_radius">



<div class="content_right_title_admin">
Thư viện ảnh
</div>   
<div class="content_right_group_admin box_shadow" style="padding-bottom:5px">
<form method="post" name="fr_themhinhanh" action="trangchu.php?key=alb" enctype="multipart/form-data" >

    <table width="60%" align="center"  cellpadding="0" cellspacing="0" class="border_radius">

        <tr>

            <td colspan="2" align="center" height="40px"><span style="font-size:18px; font-weight:bold; color:#f9d765; ">Thêm hình ảnh mới</span></td>

        </tr>

        <tr>

            <td width="30%" style="padding-left:5px;" height="30px" align="left">Tên hình ảnh<span style="color:#F00">(*)</span></td>

            <td align="left"><input type="text" name="txt_tenhinh" size="30" style="height:20px;" class="border_radius"></td>

        </tr>

        <tr>

            <td style="padding-left:5px;" height="30px" align="left">Đường dẫn<span style="color:#F00">(*)</span></td>

            <td align="left"><input type="file" name="up_hinh" size="30" style="height:20px;"  class="border_radius"></td>

        </tr>     

        

        <tr>

            <td colspan="2" align="center" style="vertical-align:middle;">

                <input type="submit" name="btn_upload" value="Thêm mới" class="button small blue" style="margin-top:10px;"/>                

            </td>

        </tr>

    </table>

</form>

<?php  		

    	include_once("../../coms/xl_hinhanh.php");

		

		$ma_album = 4;

		$ha = new xl_hinhanh();

		$arr_sp = $ha->LayHinhAnhTheoAlbum($ma_album);

		if(isset($_REQUEST["query"]) && is_numeric($_REQUEST["ma"]))

		{

			$ma_id = $_REQUEST["ma"];

			

			$hinh = new xl_hinhanh();

			$ha = $hinh->LayHinhAnhTheoMaHinhAnh($ma_album, $ma_id);

		

			unlink("../../images/".$ha[0]['duongdan']."");

			unlink("../../images/".$ha[0]['thumbnail']."");

			$xoa_hinh = new xl_hinhanh();

			$query = $xoa_hinh->XoaHinhAnh($ma_album, $ma_id);

			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=alb"  />';

		}	

?>





<script type="text/javascript">

		$(document).ready(function() {

			$("a[rel=image_group]").fancybox({

				'opacity'		: true,

				'transitionIn'	: 'elastic',

				'transitionOut'	: 'elastic'

				

			});		

		});

</script>

<table cellpadding="0" cellspacing="5" style="margin-left:3px; margin-top:0px" align="center" width="auto">

        <tr>

          <!-- Start Hình -->

          <?php

				$dem = 0;

				$start = 1;

				$limit = 12;

				$count = count($arr_sp);

				$pages = $count % $limit == 0 ? $count / $limit : ceil($count / $limit);

				if(isset($_GET["page"]))

					$start = ($_GET["page"] - 1) * $limit + 1;

				$end = ($start + $limit) < $count ? $start + $limit : $count + 1;

				/*for($i=0;$i<count($arr_sp);$i++)*/

				

				for($i = $start ; $i < $end; $i++)

				{

					

					$row_sp= $arr_sp[$i - 1];

					$duongdanchinh = DOMAIN."/images/".$row_sp['duongdan'];

			?>

          <td width="163px" >

          	<table width="100%" style="margin-top:20px" cellpadding="0" cellspacing="5">

              <tr>

                <td align="center">

                	<div style="width:100%; text-align:center">

                		<div style="padding:2px; border:solid 1px #999; width:136px; height:96px"> 

                        	<a href="../../images/<?php echo $row_sp['duongdan'];?>" title="<?php echo $row_sp['tenhinhanh']; ?>" rel="image_group"> 

                        		 <img src="../../images/<?php echo $row_sp['thumbnail'];?>" border="0" width="132" height="92"/> 

                            </a> 

							

                        </div>

                    </div>

               	</td>

              </tr>

              <tr>

                <td align="center" height="auto"><span style="color:#C60"><?php echo $row_sp['tenhinhanh'] ?></span></td>

              </tr>

              <tr>

              	

                <td align="left" height="auto">

                	<span style="font-size:10px; color:#666">Đường dẫn hình ảnh:<br></span>

                	<input type="text" name="txt_ten" value="<?php echo $duongdanchinh; ?>" class="border_radius">

                </td>

              </tr>

              <tr>

                <td align="center" height="auto">

                	<a href="trangchu.php?key=alb&query=del&ma=<?php echo $row_sp['mahinhanh'] ?>"onClick=" return confirm('Bạn có thật sự muốn xóa hình ảnh này?')" class="button small blue">Xóa</a>

                </td>

              </tr>

              

            </table>

          </td>

          <!-- End Hình -->

          <?php

					$dem++;

					if($dem == 4)

					{

						echo "</tr><tr>";

						$dem = 0;

					}

					

            	}	

				

			?>

        </tr>

      </table>

      <div class="phantrang">

      	<?php

			$self = $_SERVER["PHP_SELF"];

			if(isset($_GET["page"]))

				$page = $_GET["page"];

			$page_list = "";

			for($i=1; $i <= $pages; $i++)

			{

				$page_list .= "&nbsp;&nbsp;<a href='$self?page=$i&key=alb' class='button medium blue'>";

				if(isset($_GET["page"]))

				{

					if($page == $i)

						$page_list .= "<span style='font-size:1.1em; color:#fcf46f'><b>$i</b></span></a>&nbsp";

					else

						$page_list .= "$i</a>&nbsp;";

				}

				else

					$page_list .= "$i</a>&nbsp;";

			}

			echo $page_list;

		?>

      </div>

      

</div>



 <?php

 

if(isset($_POST['btn_upload']))

{

	$maalbum = 4;

	$tenhinhanh = $_POST['txt_tenhinh'];	

	$duongdan = "thuvienanh/".$_FILES['up_hinh']['name'];

	$thumbnail = "thuvienanh/thumbnail/thumb_".$_FILES['up_hinh']['name'];	

	$kq=array();

	$hinhanh = new xl_hinhanh();

	if($duongdan != NULL)

	{

		//$hinh_thumb = "thumb_".$file;

		$kq=up_hinh();

		//$hinh = $file;

		//$hinh_thumb = "thumb_".$kq[1];

		if($kq[2]==true)

		{

			//echo $hinh_thumb."<br>";

			//echo $hinh;

			$now = getdate();

			$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 

			$hinh_thunb=make_thumbnails($kq[0], $kq[1]);

			$ketqua = $hinhanh->ThemHinhAnh($maalbum, $tenhinhanh, $duongdan, $thumbnail, $currentDate, $_SESSION['MaNguoiDung']);

			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=alb" />';

		}

	}

	if($duongdan == NULL)

	{

		echo '<script language="javascript">

				window.alert("Không có tập tin hình ảnh. Vui lòng chọn hình ảnh!!!");

			</script>';		

		echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=alb"  />';

	}

	

	

	

}



function up_hinh()

{

	$result = "";

	define('UPLOAD_DIR','../../images/thuvienanh/');

	define('UPLOAD_DIR_THUMBNAIL','../../images/thuvienanh/thumbnail/');

	define('MAX_FILES_SIZE',10000000);

	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);

	//convert maximun size to KB

	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";

	//create an array of permitted MIME types

	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');

	//begin by assuming the file is unacceptable

	$sizeOK = false;

	$typeOK = false;

	$success = false;

	$kq=array(3);

	//Check that file is within the permitted size

	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)

	{

		$sizeOK = true;

	}

	

	//Check that file is within the permitted size

	foreach($premitted as $type)

	{

		if($type == $_FILES['up_hinh']['type'])

		{

			$typeOK = true;

			break;

		}

	}

	

	if($sizeOK && $typeOK)

	{

		switch($_FILES['up_hinh']['error'])

		{

			case 0:

				if(!file_exists(UPLOAD_DIR.$file))

				{

					//move the file to the upload folder and rename				

					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);

					// the path with the file name where the file will be stored, upload is the directory name.

					/* $dir = UPLOAD_DIR;

					echo "<script>alert('".$dir."')</script>";

					echo "<script>alert('".$file."')</script>";

					$make_thumbnails($dir, $file); */

					//CreateThumbnail(UPLOAD_DIR.$file);

					$kq[1]=$file;

					$kq[0] = UPLOAD_DIR;

					

				}

				else

				{

					$now  = date("YmdHis",time());

					$file = $now.$file;

					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);

					$kq[1]=$file;

					$kq[0] = UPLOAD_DIR;

					/* make_thumbnails(UPLOAD_DIR, $file); */

					//CreateThumbnail(UPLOAD_DIR.$now.$file);

				}

				if($success){

					$result = "$file upload thành công";

					//$result = "$file uploaded successfully";

				}

				else{

					$result = "Lỗi upload $file.Vui long thử lại.";

					//$result = "Error uploading $file.Please try again.";

				}

				break;

			case 3:

				$result = "Lỗi upload $file.Vui long thử lại.";

				//$result = "Error uploading $file.Please try again.";

			default:

				$result = "Lỗi hệ thống upload $file.Vui long thử lại.";

				//$result = "System error uploadding $file contact webmaster";

		}

	}

	elseif($_FILES['up_hinh']['error']==4){

		$result = "Chưa chọn file upload";

		//$result = "No file selected";

	}

	else{

		$result = "Lỗi file upload. Maximum size: $max. File upload phải là gif, jpg, png.";

		//$result ="$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";

	}

	if($success == TRUE)

	{

		echo '<script language="javascript">

				window.alert("'.$result.'");

			</script>';

		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';

	}

	else

	{

		echo '<script language="javascript">

				window.alert("'.$result.'");

			</script>';

		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';

	}

	$kq[2] = $success;

	return $kq;

}



function make_thumbnails($updir, $img){

	

	$thumb_preword		= "thumb_";

	

	$arr_image_details	= GetImageSize("$updir"."$img");

	$original_width		= $arr_image_details[0];

	$original_height	= $arr_image_details[1];

	

	//$thumbnail_width	= 132;

	$thumbnail_height	= 96;

	$ratio = $thumbnail_height / $original_height;

    $thumbnail_width	= $original_width * $ratio;

	

	if( $original_width > $original_height ){

		$new_width	= $thumbnail_width;

		$new_height	= intval($original_height*$new_width/$original_width);

	} else {

		$new_height	= $thumbnail_height;

		$new_width	= intval($original_width*$new_height/$original_height);

	}



	$dest_x = intval(($thumbnail_width - $new_width) / 2);

	$dest_y = intval(($thumbnail_height - $new_height) / 2);

	

	



	if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF";  }

	if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG";  }

	if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }





	if( $imgt ) { 

		$old_image	= $imgcreatefrom("$updir"."$img");

		$new_image	= imagecreatetruecolor($thumbnail_width, $thumbnail_height);

		imageCopyResized($new_image,$old_image,$dest_x, $dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

		$imgt($new_image,UPLOAD_DIR_THUMBNAIL."$thumb_preword"."$img");

	}

	return "$thumb_preword"."$img";



}



		

?>

</div>